import { Module } from 'vuex'
import { Idashboard } from './type'
import { IRootState } from '../types'
import {
  getCategoryGoodsCount,
  getCategoryGoodsSale,
  getCategoryGoodsFavor,
  getAddressGoodsSale,
  getAmountList
} from '@/service/main/dashboard/dashboard'
const dashboard: Module<Idashboard, IRootState> = {
  namespaced: true,
  state() {
    return {
      topPanelDatas: [],
      categoryGoodsCount: [],
      categoryGoodsSale: [],
      categoryGoodsFavor: [],
      addressGoodsSale: []
    }
  },
  mutations: {
    changeTopPanelDatas(state, list) {
      state.topPanelDatas = list.data
    },
    changeCategoryGoodsCount(state, list) {
      state.categoryGoodsCount = list
    },
    changeCategoryGoodsSale(state, list) {
      state.categoryGoodsSale = list
    },
    changeCategoryGoodsFavor(state, list) {
      state.categoryGoodsFavor = list
    },
    changeAddressGoodsSale(state, list) {
      state.addressGoodsSale = list
    }
  },
  actions: {
    async getDashboardDataActive({ commit }) {
      const resultTopPanelDatas = await getAmountList()
      commit('changeTopPanelDatas', resultTopPanelDatas)

      const GoodsCountResult = await getCategoryGoodsCount()
      // console.log(GoodsCountResult.data)
      commit('changeCategoryGoodsCount', GoodsCountResult.data)
      const goodsSaleResult = await getCategoryGoodsSale()
      // console.log(goodsSaleResult.data)
      commit('changeCategoryGoodsSale', goodsSaleResult.data)
      const goodsFavorResult = await getCategoryGoodsFavor()
      // console.log(goodsFavorResult.data)
      commit('changeCategoryGoodsFavor', goodsFavorResult.data)
      const addressResult = await getAddressGoodsSale()
      // console.log(addressResult.data)
      commit('changeAddressGoodsSale', addressResult.data)
    }
  }
}

export default dashboard
